﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Yule8xCommon.Applib
{
    public class RuntimeDataUtil
    {
        public static FilePathes filePathes;
        static RuntimeDataUtil()
        {
            string debugFolder;
            string appDataFolder;
            // 应用数据目录
            appDataFolder = HttpContext.Current.Server.MapPath("/Runtime/AppData/");
            // 应用调试数据目录
            debugFolder = HttpContext.Current.Server.MapPath("/Runtime/AppData/Debug/");
            filePathes = new FilePathes()
            {
                onecentusers_file = debugFolder + "onecentusers.dat",
                debugmode_file = debugFolder + "debugmode.dat",
                forbidupdategamedb_file = debugFolder + "debugupdategamedb.dat",
                welcomestr_file = appDataFolder + "welcome.dat",
                menu_file = appDataFolder + "menu.dat",
                keyword_file = appDataFolder + "keyword.dat",
                captcha_file = appDataFolder + "captcha.dat",
                nicknames_file = appDataFolder + "nicknames.dat",
                event_buydiamond_file = appDataFolder + "buydiamond_event.dat",
            };
            try
            {
                if (!Directory.Exists(debugFolder))
                {
                    Directory.CreateDirectory(debugFolder);
                }
            }
            catch (Exception ex)
            {
                LogUtil.Instance.WriteError("[RuntimeDataUtil] 静态初始化构造函数 ", ex);
            }
        }

        public static string FetchText(string filepath, string defv)
        {
            if (string.IsNullOrEmpty(filepath))
            {
                return defv;
            }
            string content = defv;
            try
            {
                if (File.Exists(filepath))
                {
                    content = File.ReadAllText(filepath);
                }
                else
                {
                    File.WriteAllText(filepath, defv);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return string.IsNullOrEmpty(content) ? defv : content;
        }

        /// <summary>
        /// 如果没有,则返回空字符串
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static string GetText(string filepath)
        {
            if (string.IsNullOrEmpty(filepath))
            {
                return string.Empty;
            }
            string content = string.Empty;
            try
            {
                if (File.Exists(filepath))
                {
                    content = File.ReadAllText(filepath);
                }
            }
            catch (Exception)
            {
                throw;
            }
            if (string.IsNullOrEmpty(content))
            {
                return string.Empty;
            }
            return content;
        }

        public static string[] GetEventBuyDiamond()
        {
            string content = GetText(filePathes.event_buydiamond_file);
            if (string.IsNullOrEmpty(content))
            {
                return null;
            }
            return content.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static bool CheckSuperCaptcha(string yzmcode)
        {
            string captcha = GetCaptchaText(string.Empty);
            if (string.IsNullOrWhiteSpace(captcha))
            {
                return false;
            }
            string[] arritems = captcha.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return arritems.Length > 0 && arritems.Contains(yzmcode);
        }
        public static string GetNickNameText()
        {
            return GetText(filePathes.nicknames_file);
        }
        public static string GetCaptchaText(string defv)
        {
            return FetchText(filePathes.captcha_file, defv);
        }

        public static string GetKeywordText(string defv = "")
        {
            return FetchText(filePathes.keyword_file, defv);
        }
        public static Dictionary<string, string> GetKeywordData(string defv = "")
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string content = GetKeywordText(defv);
            string[] arrkv = content.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            string[] arritem;
            foreach (string kv in arrkv)
            {
                arritem = kv.Split(new char[] { ':' }, 2);
                if (arritem.Length > 1)
                {
                    dict.Add(arritem[0], arritem[1]);
                }
            }
            return dict;
        }
        /// <summary>
        /// 获取欢迎语文本
        /// </summary>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static string GetWelcomeText(string defv = "")
        {
            return FetchText(filePathes.welcomestr_file, defv);
        }
        /// <summary>
        /// 获取微信菜单文本
        /// </summary>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static string GetMenuText(string defv = "")
        {
            return FetchText(filePathes.menu_file, defv);
        }
        /// <summary>
        /// 检测是否调试模式
        /// </summary>
        /// <returns></returns>
        public static bool CheckDebugModel()
        {
            string content = GetText(filePathes.debugmode_file);
            if (string.IsNullOrEmpty(content))
            {
                return false;
            }
            string content2 = AppInfo.EncryptKey + DateTime.Today.ToString("yyyyMMdd") + "True";
            content2 = MD5Util.GetShortMD5(content2);
            return content == content2;
        }

        /// <summary>
        /// 禁止更新游戏数据库
        /// <para>True=禁止;False=允许，默认允许(False)</para>
        /// </summary>
        /// <returns></returns>
        public static bool CheckForbidUpdateGameDb()
        {
            string content = GetText(filePathes.forbidupdategamedb_file);
            return !string.IsNullOrEmpty(content) && content == "True";
        }
        /// <summary>
        /// 检测用户是否符合1分钱支付的测试条件
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public static bool CheckOnecentPay(int user_id)
        {
            if (!CheckDebugModel())
            {
                return false;
            }
            string content = GetText(filePathes.onecentusers_file);
            if (string.IsNullOrEmpty(content))
            {
                return false;
            }
            string[] users = content.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return users.Contains(user_id.ToString());
        }
        /// <summary>
        /// 路径集合
        /// </summary>
        public struct FilePathes
        {
            /// <summary>
            /// 1分钱支付测试用户
            /// </summary>
            public string onecentusers_file;
            /// <summary>
            /// 是否调试模式
            /// </summary>
            public string debugmode_file;
            /// <summary>
            /// 是否禁止游戏数据库
            /// </summary>
            public string forbidupdategamedb_file;
            /// <summary>
            /// 关注微信公众号欢迎语
            /// </summary>
            public string welcomestr_file;
            /// <summary>
            /// 微信公众号菜单
            /// </summary>
            public string menu_file;
            /// <summary>
            /// 微信公众号关键词
            /// </summary>
            public string keyword_file;
            /// <summary>
            /// 超级验证码
            /// </summary>
            public string captcha_file;
            /// <summary>
            /// 昵称数据库
            /// </summary>
            public string nicknames_file;
            /// <summary>
            /// 购买钻石限时活动
            /// </summary>
            public string event_buydiamond_file;
        }
    }
}